package com.whenjun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ifootbook.util.EmptyUtil;
import com.ifootbook.util.FootBookException;
import com.whenjun.dto.isc.person.IscPersonReq;
import com.whenjun.dto.kq.KqGroupPersonReq;
import com.whenjun.dto.kq.KqGroupPersonsAddReq;
import com.whenjun.dto.person.IscPersonDTO;
import com.whenjun.entity.IscPerson;
import com.whenjun.entity.KqDepartmentGroupPerson;
import com.whenjun.mapper.KqDepartmentGroupPersonMapper;
import com.whenjun.service.IKqDepartmentGroupPersonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whenjun.util.IPageUtil;
import com.whenjun.util.SaTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whenjun
 * @since 2024-02-22
 */
@Service
public class KqDepartmentGroupPersonServiceImpl extends ServiceImpl<KqDepartmentGroupPersonMapper, KqDepartmentGroupPerson> implements IKqDepartmentGroupPersonService {

    @Autowired
    private IscPersonServiceImpl iscPersonService;

    /**
     * 考勤组人员列表
     *
     * @param kqGroupPersonReq
     * @return
     */
    @Transactional
    public List<IscPersonDTO> KqGroupPersons(KqGroupPersonReq kqGroupPersonReq) {
        MPJLambdaWrapper<IscPerson> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(IscPerson.class)
                .leftJoin(KqDepartmentGroupPerson.class, KqDepartmentGroupPerson::getPersonId, IscPerson::getPersonId)
                .eq(KqDepartmentGroupPerson::getGroupId, kqGroupPersonReq.getId())
                .eq(KqDepartmentGroupPerson::getState, false)
                .orderByDesc(KqDepartmentGroupPerson::getCreateTime)
        ;

        List<IscPersonDTO> iscPersonDTOS = iscPersonService.getBaseMapper().selectJoinList(IscPersonDTO.class, wrapper);

        return iscPersonDTOS;
    }

    @Transactional
    public IPage<IscPersonDTO> KqGroupPersonsUn(IscPersonReq iscPersonReq) {
        List<KqDepartmentGroupPerson> kqGroupPersonList = this.lambdaQuery().eq(KqDepartmentGroupPerson::getState, false).list();
        List<String> personIds = kqGroupPersonList.stream().map(KqDepartmentGroupPerson::getPersonId).collect(Collectors.toList());

        Page<IscPerson> page = new Page<>(iscPersonReq.getPage(), iscPersonReq.getSize());

        IPage<IscPerson> iscPersonList = iscPersonService.lambdaQuery()
                .notIn(!EmptyUtil.isEmpty(personIds), IscPerson::getPersonId, personIds)
                .like(!EmptyUtil.isEmpty(iscPersonReq.getPersonName()), IscPerson::getPersonName, iscPersonReq.getPersonName())
                .orderByAsc(IscPerson::getPersonName)
                .page(page)
                ;

        IPage<IscPersonDTO> iscPersonDTOIPage = IPageUtil.trans(iscPersonList);

        return iscPersonDTOIPage;
    }

    /**
     * 考勤组人员 - 新增
     * @param kqGroupPersonsAddReq
     */
    @Transactional
    public void KqGroupPersonsAdd(KqGroupPersonsAddReq kqGroupPersonsAddReq) {
        String userId = SaTokenUtil.getUserId();

        LocalDateTime now = LocalDateTime.now();
        for (String personId : kqGroupPersonsAddReq.getPersonIds()) {
            LambdaQueryWrapper<KqDepartmentGroupPerson> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(KqDepartmentGroupPerson::getPersonId, personId);
            queryWrapper.eq(KqDepartmentGroupPerson::getState, false);

            KqDepartmentGroupPerson kqGroupPerson = this.getOne(queryWrapper);
            if (kqGroupPerson == null) {
                kqGroupPerson = new KqDepartmentGroupPerson();
                kqGroupPerson.setGroupId(kqGroupPersonsAddReq.getId());
                kqGroupPerson.setPersonId(personId);
                kqGroupPerson.setState(false);
                kqGroupPerson.setCreateTime(now);
                kqGroupPerson.setUpdateTime(now);
                kqGroupPerson.setCreateUser(userId);

                this.save(kqGroupPerson);
            }else {
                throw new FootBookException("该personId: “" + personId + "” 已加入考勤组");
            }
        }
    }

    @Transactional
    public void KqGroupPersonsRm(KqGroupPersonsAddReq kqGroupPersonsAddReq) {
        String userId = SaTokenUtil.getUserId();

        LocalDateTime now = LocalDateTime.now();
        for (String personId : kqGroupPersonsAddReq.getPersonIds()) {
            LambdaQueryWrapper<KqDepartmentGroupPerson> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(KqDepartmentGroupPerson::getPersonId, personId)
                    .eq(KqDepartmentGroupPerson::getGroupId, kqGroupPersonsAddReq.getId())
                    .eq(KqDepartmentGroupPerson::getState, false)
            ;

            KqDepartmentGroupPerson kqGroupPerson = this.getOne(queryWrapper);

            if (kqGroupPerson == null) {
                continue;
            }
            kqGroupPerson.setState(true);
            kqGroupPerson.setUpdateTime(now);
            kqGroupPerson.setUpdateUser(userId);
//            this.updateById(kqGroupPerson);
            // 逻辑删除改物理删除
            this.removeById(kqGroupPerson.getId());
        }
    }

    /**
     * 删除人员byGroupId
     * @param groupId
     */
    @Transactional
    public void KqGroupPersonsRmByGroupId(String groupId) {
        String userId = SaTokenUtil.getUserId();
        // 查询groupId下 人员列表
        LambdaQueryWrapper<KqDepartmentGroupPerson> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KqDepartmentGroupPerson::getGroupId, groupId);
        this.remove(queryWrapper);
    }
}
